import request from '@/utils/request'

const api = {
  persistence: '/api/persistence'
}

/**
 * 获取持久化状态
 */
export function getPersistenceStatus() {
  return request({
    url: `${api.persistence}/status`,
    method: 'get'
  })
}

/**
 * 更新全局配置
 * @param {Object} config - 全局配置对象
 * @param {boolean} config.globalEnabled - 全局持久化开关
 * @param {string} config.strategy - 持久化策略 (all/selective/none)
 * @param {string} config.storagePath - 存储路径
 */
export function updateGlobalConfig(config) {
  return request({
    url: `${api.persistence}/global`,
    method: 'post',
    data: config
  })
}

/**
 * 更新Actor类型配置
 * @param {Object} config - Actor类型配置对象
 * @param {string} config.actorType - Actor类型完整类名
 * @param {boolean} config.enabled - 是否启用持久化
 */
export function updateActorTypeConfig(config) {
  return request({
    url: `${api.persistence}/actor-type`,
    method: 'post',
    data: config
  })
}

/**
 * 更新实例配置
 * @param {Object} config - 实例配置对象
 * @param {string} config.instanceId - 实例ID
 * @param {boolean} config.enabled - 是否启用持久化
 */
export function updateInstanceConfig(config) {
  return request({
    url: `${api.persistence}/instance`,
    method: 'post',
    data: config
  })
}

/**
 * 更新高级配置
 * @param {Object} config - 高级配置对象
 * @param {number} config.snapshotInterval - 快照间隔
 * @param {number} config.eventRetentionCount - 事件保留数量
 * @param {string} config.messageBlacklist - 消息黑名单
 */
export function updateAdvancedConfig(config) {
  return request({
    url: `${api.persistence}/advanced`,
    method: 'post',
    data: config
  })
}

/**
 * 删除Actor类型配置
 * @param {string} actorType - Actor类型
 */
export function deleteActorTypeConfig(actorType) {
  return request({
    url: `${api.persistence}/actor-type/${encodeURIComponent(actorType)}`,
    method: 'delete'
  })
}

/**
 * 删除实例配置
 * @param {string} instanceId - 实例ID
 */
export function deleteInstanceConfig(instanceId) {
  return request({
    url: `${api.persistence}/instance/${encodeURIComponent(instanceId)}`,
    method: 'delete'
  })
}

/**
 * 重置持久化配置
 */
export function resetPersistenceConfig() {
  return request({
    url: `${api.persistence}/reset`,
    method: 'post'
  })
}

/**
 * 导出持久化配置
 */
export function exportPersistenceConfig() {
  return request({
    url: `${api.persistence}/export`,
    method: 'get',
    responseType: 'blob'
  })
}

/**
 * 导入持久化配置
 * @param {FormData} formData - 包含配置文件的表单数据
 */
export function importPersistenceConfig(formData) {
  return request({
    url: `${api.persistence}/import`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}
